import { get, post, put } from '../utils/request';
import { ApiResponse, User, UserSettingsRequest, UserSettingsResponse, UserProfileResponse } from '../types';

/**
 * 获取当前用户信息
 */
export function getCurrentUser() {
  return get<ApiResponse<User>>('/user/profile');
}

/**
 * 更新用户信息
 */
export function updateUserProfile(data: {
  email?: string;
  nickname?: string;
  bio?: string;
}) {
  return put<ApiResponse<User>>('/user/profile', data);
}

/**
 * 修改密码
 */
export function changePassword(data: {
  oldPassword: string;
  newPassword: string;
}) {
  return put<ApiResponse<null>>('/user/password', data);
}

/**
 * 获取用户统计信息
 */
export function getUserStats() {
  return get<ApiResponse<{
    torrentCount: number;
    seedingCount: number;
    leechingCount: number;
    inviteCount: number;
    topicCount: number;
    todayUpload: number;
    todayDownload: number;
    weekUpload: number;
    weekDownload: number;
    monthUpload: number;
    monthDownload: number;
  }>>('/user/stats');
}

/**
 * 获取用户每日下载限制
 */
export function getDailyDownloadLimit() {
  return get<ApiResponse<number>>('/user/daily-download-limit');
}

/**
 * 获取用户公开资料
 */
export function getUserProfile(userId: number) {
  return get<ApiResponse<UserProfileResponse>>(`/user/profile/${userId}`);
}

/**
 * 重新生成Passkey
 */
export function regeneratePasskey() {
  return post<ApiResponse<string>>('/user/regenerate-passkey');
}

/**
 * 上传头像
 */
export function uploadAvatar(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return post<ApiResponse<string>>('/user/upload-avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 获取用户设置
 */
export function getUserSettings() {
  return get<ApiResponse<UserSettingsResponse>>('/user/settings');
}

/**
 * 保存用户设置
 */
export function saveUserSettings(data: UserSettingsRequest) {
  return put<ApiResponse<string>>('/user/settings', data);
} 